<?php
namespace Admin\Controller;
use Think\Controller;
class PostController extends Controller {
    public function index(){
        $this->display();
    }
    public function postlist(){
		$postmodel=M('post');
		$posts=$postmodel->order('iid ')->join('user ON post.uid=user.uid')->select();
		$this->post=$posts;
		$this->display();
		// $postmodel=M('post');
		// $posts=$postmodel->order('iid ')->select();
		// $this->post=$posts;
		// $this->display();
	}
	public function delpost($iid){
	$postmodel=M('post');	
	if (false!==$postmodel->where("iid=$iid")->join('user ON post.uid=user.uid')->delete()) {
            $this->success('删除成功！');
        } else {
            $this->error('删除失败');
        }
		
	}

    public function postadd(){
        if ($_FILES['files']['error']==0) {
                $loginfo=$this->MyUpload();
                $pname=$loginfo['file']['savename'];
                dump ($loginfo);
            }
            else{
                $pname="defalt.jpg";
            }
        $posts = D('post'); 
 
        if ( $posts->create()) {
            $data['uid']='1';
            $data['icontent'] = I('post.icontent');
            $data['postdate']=NOW_TIME;
            $data['postpicture']=$pname;
            
            if (false !== $posts->add($data)){
                 echo $posts->getLastSql();
                 // exit();
                $this->success('添加成功！', 'postlist');
            } else {         $this->error('数据错误');           }
            }else{
            $this->error($posts->getError());}
       
}
       
       public function MyUpload(){

// $_FILES["file"]["error"] =0 表示文件上传成功！！
// $_FILES["file"]["error"] =1上传的文件超过了 php.ini 中 upload_max_filesize 选项限制的值.
// $_FILES["file"]["error"] =2上传文件的大小超过了 HTML 表单中 MAX_FILE_SIZE 选项指定的值。
// $_FILES["file"]["error"] =3文件只有部分被上传
// $_FILES["file"]["error"] =4没有文件被上传
// $_FILES["file"]["error"]>0 表示文件上传错误!!!
    $upload = new \Think\Upload();// 实例化上传类
    $upload->maxSize   =     3145728 ;// 设置附件上传大小
    $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
    $upload->rootPath  =     './Public/Admin/photo/'; // 设置附件上传根目录
    $upload->autoSub   = false;//关闭子目录，默认ture
    //$info=$upload->uploadOne($_FILES['file']);  //上传文件

    //$upload->savePath  =     ''; // 设置附件上传（子）目录
    $upload->savename = 'time';

    $info   =   $upload->upload();  // 上传文件 
    //$dump(info);
    if(!$info) {        $this->error($upload->getError());    }
    else{return $info;}
    
    // else{        $this->success('上传成功！');    }  
    }
public   function editp(){

    // print_r($_POST);
    // die;//断点
        if ($_FILES['files']['error']!=0) {
                $loginfo=$this->MyUpload();
                $pname=$loginfo['file']['savename'];
                dump ($loginfo);
            }
            else{
                $pname="defalt.jpg";
            }
        $posts = M('post'); 
 
        if ( $posts->create()) {
            $iid=I('post.iid');
            $data['icontent'] = I('post.icontent');
            $data['posttime']=I('post.posttime');
            $data['postpicture']=$pname;
            
            if(false !== $posts->where("iid=$iid")->save($data)){
                 echo $posts->getLastSql();
                 // exit();
                $this->success('成功！', 'postlist');
            } else {         $this->error('数据错误');           }
            // }else{
            // $this->error($posts->getError());
             }
        }
    public function edit(){    
            $posts = D('post'); 
            $iid=$_GET['iid'];
            $res= $posts->where("iid=$iid")->find();
            $this->post=$res;
         
        $this->display();
        }

        public   function editt(){

    // print_r($_POST);
    // die;//断点
        if ($_FILES['files']['error']!=0) {
                $loginfo=$this->MyUpload();
                $pname=$loginfo['file']['savename'];
                dump ($loginfo);
            }
            else{
                $pname="defalt.jpg";
            }
        $posts = D('post'); 
 
        if ( $posts->create()) {
            $iid=I('post.iid');
            $data['icontent'] = I('post.icontent');
            $data['posttime']=I('post.posttime');
            $data['postpicture']=$pname;
            dump($data);
            if(false !== $posts->where("iid=$iid")->save($data)){
                 echo $posts->getLastSql();
                 // exit();
                $this->success('成功！', 'postlist');
            } else {         $this->error('数据错误');           }
            // }else{
            // $this->error($posts->getError());
             }
        }
}

    
